<?php
/* SVN FILE: $Id: item_pintopic.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Open/Close Topic item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/topic/item_pintopic.php $
*/

class item_pintopic extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Pin/Unpin a Topic";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Use this item to pin an unpinned topic or unpin a pinned topic.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Item specific setting
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $itemSettings = array();
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
		}
		else
		{
			$this->itemSettings['no_forum'] = array();
			$this->itemSettings['limit'] = 1;
		}
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		require_once( IPSLib::getAppDir( 'forums' ) . "/sources/classes/forums/class_forums.php" );
		require_once( IPSLib::getAppDir( 'forums' ) . '/sources/classes/forums/admin_forum_functions.php' );

		$forum_functions = new admin_forum_functions( $this->registry );
		$forum_functions->forumsInit();
		$forumlist = $forum_functions->adForumsForumList();
		
		$form['no_forum'] = ipsRegistry::getClass('output')->formMultiDropdown('item[no_forum][]', $forumlist, $this->itemSettings['itemno_forums']);
		$form['limit'] = ipsRegistry::getClass('output')->formYesNo('item[limit]', $this->itemSettings['limit']);
		return <<<EOF
				<li>
					<label>{$this->lang->words['no_forums_applied']}</label>
					{$form['no_forum']}
				</li>
				<li>
					<label>{$this->lang->words['limit_to_own_topic']}</label>
					{$form['limit']}
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @todo
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['no_forum'] = is_array($input['no_forum']) ? $input['no_forum'] : array();
		$setting['limit'] = intval($input['limit']) & 1;
		
		$item_info['item_config'] = serialize($setting);
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		if($this->request['request_method'] == 'post')
		{
			require_once( IPSLib::getAppDir( 'forums' ) . "/sources/classes/forums/class_forums.php" );
			$class_forums = new class_forums($this->registry);
			$class_forums->forumsInit();
			
			$topic_id = 0;
			if(is_numeric($this->request['topic']))
			{
				$topic_id = intval($this->request['topic']);
			}
			if(!$topic_id)
			{
				if(preg_match("#showtopic=([0-9]+)#",$this->request['topic'],$match))
				{
					$topic_id = $match[1];
				}
				elseif(preg_match("#/topic/(\d+?)-#i",$this->request['topic'],$match))
				{
					$topic_id = $match[1];
				}
				else
				{
					$this->registry->getClass('output')->showError('topic_not_found');
				}
			}
			if(!$topic = $this->DB->buildAndFetch(array('select'=>'*','from'=>'topics','where'=>"tid=".$topic_id)))
			{
				$this->registry->getClass('output')->showError('topic_not_found');
			}
			if($topic['approved'] != 1)
			{
				$this->registry->getClass('output')->showError('topic_not_found');
			}
			if(!$class_forums->forumsCheckAccess($topic['forum_id'], 0, 'topic', $topic, true))
			{
				$this->registry->getClass('output')->showError('topic_not_found');
			}
			if(in_array($topic['forum_id'],$this->itemSettings['no_forum']))
			{
				$this->registry->getClass('output')->showError('topic_not_found');
			}
			if($this->itemSettings['limit'] && $topic['starter_id'] != $this->memberData['member_id'])
			{
				$this->registry->getClass('output')->showError('topic_not_own');
			}
			
			$this->DB->update('topics',array(
												'pinned' => intval(!$topic['pinned'])
							),"tid=".$topic_id);
			
			$this->deleteItems($invItem);
			
			$this->lang->words['topic_state_changed'] = sprintf($this->lang->words['topic_state_changed'], $topic['title']);
			
			$this->writeLog($invItem, ($topic['pinned'] ? 'Unpin' : 'Pin')." topic <a href='{$this->settings['base_url']}showtopic={$topic['tid']}'>{$topic['title']}</a>");
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['topic_state_changed'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		return <<<EOF
<form id='openTopicForm' method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='openTopic'class='block_wrap'>
		<h3 id='openTopicHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class="row2">
			<ul>
				<li>
					<label>{$this->lang->words['topic_url']}</label>
					<input type='text' name='topic' value='' size='64' id='topic' class='input_text' />
				</li>
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['open_close_topic_submit']}" name="submit" />
		</fieldset>
	</div>
</form>
EOF;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
